﻿using System.Collections.Generic;
using Missbot.Service;
using Dolany.UtilityTool;

namespace Missbot.AIModule.OnlineStore
{
    public partial class OSPerson : DbBaseEntity
    {
        public string UserID { get; set; }

        public int Golds { get; set; }

        public int Diamonds { get; set; }

        public Dictionary<string, int> GiftDic { get; set; } = new();

        public List<string> HonorNames { get; set; } = new();
    }

    public static class OSPersonDA
    {
        public static OSPerson GetPerson(string UserID)
        {
            var osPerson = MongoService<OSPerson>.GetOnly(p => p.UserID == UserID);
            if (osPerson == null)
            {
                osPerson = new OSPerson {UserID = UserID};
                MongoService<OSPerson>.Insert(osPerson);
            }

            return osPerson;
        }

        public static void GiftIncome(this OSPerson entity, string GiftName, int count = 1)
        {
            entity.GiftDic ??= new Dictionary<string, int>();

            if (entity.GiftDic.ContainsKey(GiftName))
            {
                entity.GiftDic[GiftName] += count;
            }
            else
            {
                entity.GiftDic.Add(GiftName, count);
            }
        }

        public static void GoldIncome(string UserID, int gold)
        {
            var osPerson = GetPerson(UserID);

            osPerson.Golds += gold;
            osPerson.Update();
        }

        public static int GoldConsume(string UserID, int gold)
        {
            var person = GetPerson(UserID);
            person.Golds -= gold;
            person.Update();

            return person.Golds;
        }

        public static void Update(this OSPerson entity)
        {
            entity.GiftDic.Remove(g => g == 0);
            MongoService<OSPerson>.Update(entity);
        }
    }
}